package com.ljs.mongodemo.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.ljs.mongodemo.demo.bean.User;
import com.ljs.mongodemo.demo.service.MongodbUserService;
import com.ljs.mongodemo.demo.util.DateUtils;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class MongoTestController {
    @Resource
    MongodbUserService mongodbUserService;
    @PostMapping("/save")
    public String saveUser(@RequestBody User user) {
//        user=new User();
//        user.setId(2l);
//        user.setUserName("ljs");
//        user.setNote("第一次测试user");
        String idstr=UUID.randomUUID().toString();

        user.setId(idstr);
        String dateStr="";
        Date date=new Date();
        dateStr=  DateUtils.getDateFormatString(date,"yyyy-MM-dd HH:mm:ss");

        user.setCreateTime(dateStr);
        //判断id
   if(!StringUtils.isBlank(user.getUserName())){
       User exit_user=  mongodbUserService.getUserByname(user.getUserName());
       //存在
       if(exit_user!=null){
           return "已经存在";
       }else{
           //再保存
           mongodbUserService.saveUser(user);
           return user.toString();
       }
   }else{
       return "参数出错";
   }




    }
    @GetMapping("/get/{id}")
    public User get(@PathVariable("id")  String id){
        User user=mongodbUserService.getUser(id);
        return user;
    }
   @PostMapping("/upd")
    public UpdateResult upd(@RequestBody User user){

       UpdateResult updateResult=mongodbUserService.updateUser(user);
       return updateResult;
   }
   @PostMapping("/del/{id}")
    public DeleteResult del(@PathVariable("id") String id){
        return  mongodbUserService.deleteUser( id);
   }
    @PostMapping("/find/{userName}/{note}/{skip}/{limit}")

    public List<User> findUser(@PathVariable("userName") String userName,@PathVariable("note") String note,@PathVariable("skip") Integer skip,@PathVariable("limit") Integer limit) {
        List<User> userList = mongodbUserService.findUser(userName, note, skip, limit);
        return userList;
    }
    @PostMapping("/findall/{skip}/{limit}")

    public List<User> findallUser(@PathVariable("skip") Integer skip,@PathVariable("limit") Integer limit) {
        List<User> userList = mongodbUserService.findall(skip, limit);

        return userList;
    }
    @PostMapping("/selBygrup/{id}")
    public AggregationResults aggregationResults(@PathVariable("id") Long id){
        Criteria criteria=Criteria.where("id").gte(id);
        Criteria criteria2=Criteria.where("user_name").regex("张");
        Sort sort= Sort.by(Sort.Direction.ASC,"id");
        AggregationResults aggregationResults=   mongodbUserService.aggregation(3,2,criteria,criteria2,"id","user_name","分组",sort,"user");
        return aggregationResults;
    }
//    @RequestMapping("/selBytype/{type}")
//    public    List<String> sel(@PathVariable("type") String type){
//        JSONArray jsonArray=new JSONArray();
//      // List<String> userlist= mongodbUserService.selbyType(type );
//
//       // return userlist;
//    }

}
